package com.weilai.service.serviceImpl;

import com.weilai.controller.exceptionHandler.BusinessException;
import com.weilai.controller.exceptionHandler.Code;
import com.weilai.controller.exceptionHandler.SystemException;
import com.weilai.dao.BackgroundDao;
import com.weilai.dao.UserDao;
import com.weilai.domain.Background;
import com.weilai.domain.User;
import com.weilai.service.BackgroundService;
import com.weilai.service.UserService;
import com.weilai.util.UploadUtil;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;

@Service
public class BackgroundServiceImpl implements BackgroundService {

    @Autowired
    private BackgroundDao backgroundDao;

    @Autowired
    private UserService userService;

    //根据用户id查询背景url
    @Override
    public Background getByUserId(Integer userId) {
        try{
            User user = userService.getUserById(userId);
            if (user == null) {
                throw new BusinessException(
                        Code.PROJECT_DATA_NOT_FOUND,
                        "用户不存在"
                );
            }
            return backgroundDao.getByUserId(userId);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            throw new BusinessException(Code.SYSTEM_DB_ERROR, "查询背景时数据库出错");
        }
    }

    //更改背景
    @Override
    public void updateBackground(Integer userId,MultipartFile file) {
        try{
            User user = userService.getUserById(userId);
            if (user == null) {
                throw new BusinessException(
                        Code.PROJECT_DATA_NOT_FOUND,
                        "用户不存在"
                );
            }
            // 参数校验
            if (file.isEmpty()) {
                throw new BusinessException(
                        Code.PROJECT_VALIDATE_ERROR,
                        "请上传背景"
                );
            }

            // 文件类型校验
            String[] allowedTypes = {"image/jpeg", "image/png", "application/pdf"};
            if (!Arrays.asList(allowedTypes).contains(file.getContentType())) {
                throw new BusinessException(
                        Code.PROJECT_VALIDATE_ERROR,
                        "上传失败：仅支持 JPG/PNG/PDF 文件"
                );
            }

            // 文件大小校验
            if (file.getSize() > 5 * 1024 * 1024) {
                throw new BusinessException(
                        Code.PROJECT_VALIDATE_ERROR,
                        "图片大小不能超过5MB"
                );
            }
            Background background = backgroundDao.getByUserId(userId);
            String fileName = UploadUtil.upload(file);
            background.setUrl(fileName);
            backgroundDao.updateBackground(background);
        } catch (BusinessException e) {
            throw e;
        } catch (IOException e) {
            throw new SystemException(
                    Code.SYSTEM_DB_ERROR,
                    "背景上传失败",
                    e
            );
        } catch (Exception e) {
            throw new SystemException(Code.SYSTEM_UNKNOWN_ERROR, "系统异常，请稍后重试", e);
        }
    }

    //注册背景
    @Override
    public void insertBackground(int userId, String url){
        try{
            backgroundDao.insertBackground(userId, url);
        } catch (Exception e) {
            throw new BusinessException(Code.SYSTEM_DB_ERROR, "背景注册时数据库出错");
        }
    }
}
